﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Excel;
namespace BYManageInfoSystem
{

    public partial class Frm_DepMonthOutGoodsStat : Form
    {
        private DGBY.BLL.FacFeeTab m_bllFacFeeTab = new DGBY.BLL.FacFeeTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();

        public class CStatInfo
        {
            public Decimal dtJinE = 0;
            public string strBiZhi = "";
            public string strBYComp = "";
        }

        public class CStatInfoEx
        {
            public Dictionary<int, CStatInfo> dicMonth2CStatInfo;
            public string strPerson = "";
            
           
        }

        Dictionary<string, CStatInfoEx> m_dicFac2StatInfoList = new Dictionary<string, CStatInfoEx>();

        public Frm_DepMonthOutGoodsStat()
        {
            InitializeComponent();
        }
        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }
        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
        }

        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
        private void MakeFirstColumn(DataTable dt)
        {
            dt.Columns.Add("序号", System.Type.GetType("System.Int32"));
            dt.Columns.Add("客户", System.Type.GetType("System.String"));
            dt.Columns.Add("跟单员", System.Type.GetType("System.String"));

            for (int i = 0; i < 6; i++)
            {
                dt.Columns.Add("进出口" + i.ToString(), System.Type.GetType("System.Decimal"));
                dt.Columns.Add("经贸" + i.ToString(), System.Type.GetType("System.Decimal"));
            }


            dt.Columns.Add("分厂合计", System.Type.GetType("System.Decimal"));
        }
        private void ShowAllRecordInDataGridView()
        {
            ultraGrid1.DataSource = null;
            int nYear = this.dateTimePickerSBDate.Value.Year;
            
            for(int k = 1; k < 13; k++)
            {
                DateTime dt = new DateTime(nYear, k, 1);
                DateTime dtFirst = FirstDayOfMonth(dt);
                DateTime dtEnd = LastDayOfMonth(dt);
                DataSet ds = m_bllFacFeeTab.ShowMonthDepBGJinERec("BYDepartTab.BYDepartTabID = '" + this.comboBoxDep.SelectedValue + "'"
                                                                   + @" and BGDComeBackTab.SBDate >= '" + dtFirst.Date + "'"
                                     + " and BGDComeBackTab.SBDate <= '" + dtEnd.Date + "'");

                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        string strPerson = ds.Tables[0].Rows[i]["跟单人"].ToString();
                        string strFac = ds.Tables[0].Rows[i]["工厂名称"].ToString();
                        string strByComp = ds.Tables[0].Rows[i]["经营单位"].ToString();
                        string strBiZhi = ds.Tables[0].Rows[i]["币种"].ToString();
                        Decimal dtTemp = Convert.ToDecimal(ds.Tables[0].Rows[i]["报关金额"].ToString());


                        CStatInfo csi = new CStatInfo();
                        csi.dtJinE = dtTemp;
                        csi.strBiZhi = strBiZhi;
                        csi.strBYComp = strByComp;
                        if(m_dicFac2StatInfoList.Keys.Contains(strFac))
                        {
                            m_dicFac2StatInfoList[strFac].dicMonth2CStatInfo.Add(k, csi);
                        }
                        else
                        {
                            CStatInfoEx csie = new CStatInfoEx();
                            
                            csie.strPerson = strPerson;
                            csie.dicMonth2CStatInfo = new Dictionary<int, CStatInfo>();
                            csie.dicMonth2CStatInfo.Add(k, csi);
                            
                            m_dicFac2StatInfoList.Add(strFac, csie);
                        }
                    }
                }
           
            }

            DataTable dtNew = new DataTable();
            MakeFirstColumn(dtNew);

            int nSeq = 0;
            foreach (string strTemp in m_dicFac2StatInfoList.Keys)
            {
                nSeq++;
                DataRow drTotal = dtNew.NewRow();
                drTotal["序号"] = nSeq.ToString();
                drTotal["客户"] = strTemp;
                drTotal["跟单员"] = m_dicFac2StatInfoList[strTemp].strPerson;

                Decimal dtOne2NineJCC = 0;
                Decimal dtOne2NineXJM = 0;
                for (int i = 1; i < 10; i++)
                {

                    if (m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo.Keys.Contains(i))
                    {
                        Decimal dtTemp = Convert2Dorlar(m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo[i].strBiZhi
                                        , m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo[i].dtJinE);
                        if (m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo[i].strBYComp.IndexOf("进出口") >= 0)
                        {
                            dtOne2NineJCC += dtTemp;
                        }
                        else
                        {
                            dtOne2NineXJM += dtTemp;
                        }
                    }

                }

                drTotal["进出口0"] = dtOne2NineJCC;
                drTotal["经贸0"] = dtOne2NineXJM;

                Decimal dtTen2TwlveJCC = 0;
                Decimal dtTen2TwlveXJM = 0;
                for (int i = 10; i < 13; i++)
                {
                    if (m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo.Keys.Contains(i))
                    {
                        Decimal dtTemp = Convert2Dorlar(m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo[i].strBiZhi
                                        , m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo[i].dtJinE);
                        if (m_dicFac2StatInfoList[strTemp].dicMonth2CStatInfo[i].strBYComp.IndexOf("进出口") >= 0)
                        {
                            drTotal["进出口" + (i - 9).ToString()] = dtTemp;
                            drTotal["经贸" + (i - 9).ToString()] = 0;
                            dtTen2TwlveJCC += dtTemp;
                        }
                        else
                        {
                            drTotal["进出口" + (i - 9).ToString()] = 0;
                            drTotal["经贸" + (i - 9).ToString()] = dtTemp;
                            dtTen2TwlveXJM += dtTemp;
                        }
                        
                    }
                    else
                    {
                        drTotal["进出口" + (i - 9).ToString()] = 0;
                        drTotal["经贸" + (i - 9).ToString()] = 0;
                    }
                }

                drTotal["进出口4"] = dtTen2TwlveJCC;
                drTotal["经贸4"] = dtTen2TwlveXJM;


                drTotal["进出口5"] = dtOne2NineJCC + dtTen2TwlveJCC;
                drTotal["经贸5"] = dtOne2NineXJM + dtTen2TwlveXJM;

                drTotal["分厂合计"] = dtOne2NineJCC + dtTen2TwlveJCC + dtOne2NineXJM + dtTen2TwlveXJM;


                dtNew.Rows.Add(drTotal.ItemArray);
            }


            ultraGrid1.DataSource = dtNew;
            ProcessMultiHeader();
            
        }
        private void ProcessMultiHeader()
        {
            int nSpanX = 0;
            int nOriginX = 0;
            int nOriginXTemp = 0;
            bool bSpan = false;
            UltraGridColumn ultraGridColumn1 = null;
            UltraGridColumn ultraGridColumn2 = null;
            UltraGridColumn ultraGridColumn3 = null;
            UltraGridColumn ultraGridColumn4 = null;
            UltraGridColumn ultraGridColumn5 = null;
            UltraGridColumn ultraGridColumn6 = null;
            foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                //if (str == "总金额")
                //    bSpan = false;

                if (!bSpan)
                {
                    if (str == "1-3季度" || str == "10月份" || str == "11月份"
                        || str == "12月份" || str == "第四季度合计" || str == "本年累计")
                        continue;
                    column2.RowLayoutColumnInfo.SpanY = 4;
                    if (column2.Hidden == false)
                        nOriginXTemp += 2;
                }
                else
                {
                    nSpanX += 2;
                    column2.RowLayoutColumnInfo.OriginY = 2;
                }
                if (str == "跟单员")
                {
                    bSpan = true;
                    nOriginX = nOriginXTemp;
                }
            }

            ultraGridColumn1 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("1-3季度", "1-3季度");
            ultraGridColumn1.CellActivation = Activation.Disabled;
            ultraGridColumn1.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn1.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn1.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn1.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn1.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            ultraGridColumn1.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn1.RowLayoutColumnInfo.SpanX = 4;
            ultraGridColumn1.RowLayoutColumnInfo.SpanY = 2;

            ultraGridColumn2 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("10月份", "10月份");
            ultraGridColumn2.CellActivation = Activation.Disabled;
            ultraGridColumn2.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn2.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn2.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn2.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn2.RowLayoutColumnInfo.OriginX = nOriginX + 4;//有2个列隐藏了
            ultraGridColumn2.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn2.RowLayoutColumnInfo.SpanX = 4;
            ultraGridColumn2.RowLayoutColumnInfo.SpanY = 2;

            ultraGridColumn3 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("11月份", "11月份");
            ultraGridColumn3.CellActivation = Activation.Disabled;
            ultraGridColumn3.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn3.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn3.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn3.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn3.RowLayoutColumnInfo.OriginX = nOriginX + 4 + 4;//有2个列隐藏了
            ultraGridColumn3.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn3.RowLayoutColumnInfo.SpanX = 4;
            ultraGridColumn3.RowLayoutColumnInfo.SpanY = 2;

            ultraGridColumn4 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("12月份", "12月份");
            ultraGridColumn4.CellActivation = Activation.Disabled;
            ultraGridColumn4.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn4.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn4.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn4.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn4.RowLayoutColumnInfo.OriginX = nOriginX + 4 + 4 + 4;//有2个列隐藏了
            ultraGridColumn4.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn4.RowLayoutColumnInfo.SpanX = 4;
            ultraGridColumn4.RowLayoutColumnInfo.SpanY = 2;

            ultraGridColumn5 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("第四季度合计", "第四季度合计");
            ultraGridColumn5.CellActivation = Activation.Disabled;
            ultraGridColumn5.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn5.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn5.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn5.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn5.RowLayoutColumnInfo.OriginX = nOriginX + 4 + 4 + 4 + 4;//有2个列隐藏了
            ultraGridColumn5.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn5.RowLayoutColumnInfo.SpanX = 4;
            ultraGridColumn5.RowLayoutColumnInfo.SpanY = 2;

            ultraGridColumn6 = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("本年累计", "本年累计");
            ultraGridColumn6.CellActivation = Activation.Disabled;
            ultraGridColumn6.SortIndicator = SortIndicator.Disabled;
            ultraGridColumn6.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            ultraGridColumn6.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            ultraGridColumn6.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            ultraGridColumn6.RowLayoutColumnInfo.OriginX = nOriginX + 4 + 4 + 4 + 4 + 4;//有2个列隐藏了
            ultraGridColumn6.RowLayoutColumnInfo.OriginY = 0;
            ultraGridColumn6.RowLayoutColumnInfo.SpanX = 6;
            ultraGridColumn6.RowLayoutColumnInfo.SpanY = 2;
        }
        private Decimal Convert2Dorlar(string strBiZhi, Decimal dtJinE)
        {
            return dtJinE;
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void Frm_DepMonthOutGoodsStat_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
            DataSet ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
    }
}
